package com.doubao.device.service.impl;

import com.doubao.common.result.Result;
import com.doubao.device.dto.VoiceConfigDTO;
import com.doubao.device.entity.SmartDevice;
import com.doubao.device.exception.ResourceNotFoundException;
import com.doubao.device.mapper.SmartDeviceMapper;
import com.doubao.device.service.DeviceVoiceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能体语音服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DeviceVoiceServiceImpl implements DeviceVoiceService {

    private final SmartDeviceMapper deviceMapper;

    @Value("${doubao.device.default-voice-id:zh_female_voice}")
    private String defaultVoiceId;

    // 初始化语音类型映射
    private static final Map<String, VoiceConfigDTO> VOICE_TYPES = new HashMap<>();

    static {
        // 初始化一些常用的语音类型
        addVoiceType("zh_female_qingxinnvsheng_mars_bigtts", "清新女声", "female", "zh", "这是一个清新自然的女性声音。");
        addVoiceType("zh_male_wenzhongnanshen_mars_bigtts", "温中男声", "male", "zh", "这是一个温和稳重的男性声音。");
        addVoiceType("zh_female_zhimiaonvsheng_mars_bigtts", "知妙女声", "female", "zh", "这是一个知性有魅力的女性声音。");
        addVoiceType("zh_female_remengnvsheng_mars_bigtts", "热萌女声", "female", "zh", "这是一个活泼可爱的女性声音。");
        addVoiceType("zh_male_fenggunnanshen_mars_bigtts", "风骨男声", "male", "zh", "这是一个有风骨气质的男性声音。");
        addVoiceType("zh_male_youthvoice_mars_bigtts", "青少年男声", "male", "zh", "这是一个年轻活力的男性声音。");
        addVoiceType("en_female_professional", "英语专业女声", "female", "en", "This is a professional female voice for English.");
        addVoiceType("en_male_professional", "英语专业男声", "male", "en", "This is a professional male voice for English.");
    }

    private static void addVoiceType(String voiceType, String voiceName, String gender, String language, String sampleText) {
        VoiceConfigDTO config = new VoiceConfigDTO();
        config.setVoiceType(voiceType);
        config.setVoiceName(voiceName);
        config.setGender(gender);
        config.setLanguage(language);
        config.setProvider("volcano_tts");
        config.setSpeedRatio(1.0f);
        config.setPitch(1.0f);
        config.setVolume(1.0f);
        config.setSampleText(sampleText);
        VOICE_TYPES.put(voiceType, config);
    }

    @Override
    public Result<VoiceConfigDTO> getDeviceVoiceConfig(Long deviceId) {
        SmartDevice device = deviceMapper.selectById(deviceId);
        if (device == null) {
            return Result.failed("智能体不存在");
        }

        VoiceConfigDTO voiceConfig = new VoiceConfigDTO();

        // 基本配置
        voiceConfig.setVoiceId(device.getVoiceId());
        voiceConfig.setVoiceType(device.getVoiceType());
        voiceConfig.setSpeedRatio(device.getVoiceSpeedRatio() != null ? device.getVoiceSpeedRatio() : 1.0f);

        // 根据类型获取其他配置信息
        if (device.getVoiceType() != null && VOICE_TYPES.containsKey(device.getVoiceType())) {
            VoiceConfigDTO typeConfig = VOICE_TYPES.get(device.getVoiceType());
            voiceConfig.setVoiceName(typeConfig.getVoiceName());
            voiceConfig.setGender(typeConfig.getGender());
            voiceConfig.setLanguage(typeConfig.getLanguage());
            voiceConfig.setProvider(typeConfig.getProvider());
            voiceConfig.setPitch(typeConfig.getPitch());
            voiceConfig.setVolume(typeConfig.getVolume());
            voiceConfig.setSampleText(typeConfig.getSampleText());
        } else {
            // 使用默认值
            VoiceConfigDTO defaultConfig = getDefaultVoiceConfig();
            voiceConfig.setVoiceName(defaultConfig.getVoiceName());
            voiceConfig.setGender(defaultConfig.getGender());
            voiceConfig.setLanguage(defaultConfig.getLanguage());
            voiceConfig.setProvider(defaultConfig.getProvider());
            voiceConfig.setPitch(defaultConfig.getPitch());
            voiceConfig.setVolume(defaultConfig.getVolume());
            voiceConfig.setSampleText(defaultConfig.getSampleText());
        }

        return Result.success(voiceConfig);
    }

    @Override
    public Result<Void> updateDeviceVoiceConfig(Long deviceId, VoiceConfigDTO voiceConfig) {
        SmartDevice device = deviceMapper.selectById(deviceId);
        if (device == null) {
            return Result.failed("智能体不存在");
        }

        try {
            // 更新语音配置
            device.setVoiceId(voiceConfig.getVoiceId());
            device.setVoiceType(voiceConfig.getVoiceType());
            device.setVoiceSpeedRatio(voiceConfig.getSpeedRatio());

            deviceMapper.updateById(device);
            return Result.success();
        } catch (Exception e) {
            log.error("更新智能体语音配置失败", e);
            return Result.failed("更新智能体语音配置失败: " + e.getMessage());
        }
    }

    @Override
    public Result<List<VoiceConfigDTO>> getAvailableVoiceTypes() {
        List<VoiceConfigDTO> voiceTypes = new ArrayList<>(VOICE_TYPES.values());
        return Result.success(voiceTypes);
    }

    @Override
    public Result<VoiceConfigDTO> getVoiceConfigByType(String voiceType) {
        if (voiceType == null || !VOICE_TYPES.containsKey(voiceType)) {
            return Result.failed("语音类型不存在");
        }
        return Result.success(VOICE_TYPES.get(voiceType));
    }

    @Override
    public VoiceConfigDTO getDefaultVoiceConfig() {
        // 使用默认语音配置
        VoiceConfigDTO defaultConfig = new VoiceConfigDTO();
        defaultConfig.setVoiceId(defaultVoiceId);
        defaultConfig.setVoiceType("zh_female_qingxinnvsheng_mars_bigtts");
        defaultConfig.setVoiceName("默认清新女声");
        defaultConfig.setGender("female");
        defaultConfig.setLanguage("zh");
        defaultConfig.setProvider("volcano_tts");
        defaultConfig.setSpeedRatio(1.0f);
        defaultConfig.setPitch(1.0f);
        defaultConfig.setVolume(1.0f);
        defaultConfig.setSampleText("这是默认语音的示例文本。");
        return defaultConfig;
    }
}